package com.zx.platform.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * @author zhanglk
 *
 */
public class ZipUtils {
	private static final int BUFFER = 2048;

	
	
	/**
	 * @author zhanglk
	 * @param zipFileName  
	 * @param inputFileList  文件名集合包括路径
	 * @return 压缩后文件的路径
	 * @throws Exception
	 */
	public static void filesZip(String zipFileName, List inputFileList) throws Exception {
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
				zipFileName));
		if(inputFileList!=null&&inputFileList.size()>0){
			for(int i=0;i<inputFileList.size();i++){
				File inputFile = new File(inputFileList.get(i).toString());
				zip(out,inputFile , inputFile.getName());
			}
		}
		System.out.println("zip done");
		out.close();
	}
	
	private static void zip(ZipOutputStream out, File f, String base) throws Exception {
		if (f.isDirectory()) {
			File[] fl = f.listFiles();
			//out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
			//用ant的包会支持中文，在此可能不支持中文 edit by wenjw 2016.8.28
			out.putNextEntry(new ZipEntry(base + "/"));
			base = base.length() == 0 ? "" : base + "/";
			System.out.println(base);
			for (int i = 0; i < fl.length; i++) {
				zip(out, fl[i], base + fl[i].getName());
			}
		} else {
			//out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
			//用ant的包会支持中文，在此可能不支持中文 edit by wenjw 2016.8.28
			out.putNextEntry(new ZipEntry(base));
			FileInputStream in = new FileInputStream(f);
			int count;
			byte data[] = new byte[BUFFER];
			while ((count = in.read(data, 0, BUFFER)) != -1) {
				out.write(data, 0, count);
			}
			in.close();
		}
	}
/**
 * 解压缩
 * @param fileName zip文件
 * @param filePath 解压缩后的路径
 */
	public boolean unzip(String fileName, String filePath) {
		try {
			ZipFile zipFile = new ZipFile(fileName);
			Enumeration emu = zipFile.entries();
			while (emu.hasMoreElements()) {
				ZipEntry entry = (ZipEntry) emu.nextElement();
				// 会把目录作为一个file读出一次，所以只建立目录就可以，之下的文件还会被迭代到。
				if (entry.isDirectory()) {
					new File(filePath + entry.getName()).mkdirs();
					continue;
				}
				BufferedInputStream bis = new BufferedInputStream(zipFile
						.getInputStream(entry));
				File file = new File(filePath + entry.getName());
				// 加入这个的原因是zipfile读取文件是随机读取的，这就造成可能先读取一个文件
				// 而这个文件所在的目录还没有出现过，所以要建出目录来。
				File parent = file.getParentFile();
				if (parent != null && (!parent.exists())) {
					parent.mkdirs();
				}
				FileOutputStream fos = new FileOutputStream(file);
				BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

				int count;
				byte data[] = new byte[BUFFER];
				while ((count = bis.read(data, 0, BUFFER)) != -1) {
					bos.write(data, 0, count);
				}
				bos.flush();
				bos.close();
				bis.close();
			}
			zipFile.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
			
		}
		return true;
	}
	public static void main(String[] args) 
	{
		String zipPath = "e:\\wans";
		String dir = "e:\\";
		String zipFileName = "wans.zip";
		List pathList = new ArrayList();
		File file = new File(zipPath);
		pathList.add(file);
		try
		{
			filesZip(dir+zipFileName,pathList);
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		String zipFilePath = "D:\\ziptest\\zipPath\\test.zip";
		String unzipFilePath = "D:\\ziptest\\zipPath";
		try 
		{
			//unzip(zipFilePath, unzipFilePath, true);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
